﻿namespace ShortcutReader
{
    using System.Configuration;
    using System.Linq;
    using System.Windows;

    using Microsoft.Win32;

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainView : Window
    {
        #region Constructors

        public MainView()
        {
            InitializeComponent();
            this.DataContext = new MainViewModel();
        }

        #endregion Constructors

        #region Properties

        private MainViewModel ViewModel
        {
            get
            {
                return (MainViewModel)this.DataContext;
            }
        }

        #endregion Properties

        #region Methods

        private void Click_Open(object sender, RoutedEventArgs e)
        {
            var dialogue = new OpenFileDialog();
            dialogue.DefaultExt = ".txt";
            dialogue.Filter = "Shortcuts (.lnk)|*.lnk";

            var dr = dialogue.ShowDialog();

            if (dr != true) { return; }

            this.ViewModel.ShortcutPath = dialogue.FileName;
            this.ViewModel.OpenShortcut();
        }

        private void Click_Save(object sender, RoutedEventArgs e)
        {
            this.ViewModel.SaveShortcut();
        }

        private void Window_Drop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent("FileName"))
            {
                this.ViewModel.ShortcutPath = (e.Data.GetData("FileName") as string[]).ElementAt(0);
                this.ViewModel.OpenShortcut();
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            bool result;
            bool.TryParse(ConfigurationManager.AppSettings["ShowOpen"], out result);

            Btn_Open.Visibility = (result) ? Visibility.Visible : Visibility.Collapsed;
        }

        #endregion Methods
    }
}